home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Common / include / d3dapp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  9.0 KB  |  212 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DApp.h
  3. //
  4. // Desc: Application class for the Direct3D samples framework library.
  5. //
  6. // Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef D3DAPP_H
  9. #define D3DAPP_H
  10.  
  11. // basetsd.h defines INT_PTR (used below).  It is not included by default
  12. // under VC 5.0.  If you are using VC6 or later, it is included via Windows.h.
  13. #include <basetsd.h> 
  14.  
  15.  
  16. //-----------------------------------------------------------------------------
  17. // Error codes
  18. //-----------------------------------------------------------------------------
  19. enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
  20.  
  21. #define D3DAPPERR_NODIRECT3D          0x82000001
  22. #define D3DAPPERR_NOWINDOW            0x82000002
  23. #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
  24. #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
  25. #define D3DAPPERR_NOHARDWAREDEVICE    0x82000005
  26. #define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006
  27. #define D3DAPPERR_NOWINDOWEDHAL       0x82000007
  28. #define D3DAPPERR_NODESKTOPHAL        0x82000008
  29. #define D3DAPPERR_NOHALTHISMODE       0x82000009
  30. #define D3DAPPERR_NONZEROREFCOUNT     0x8200000a
  31. #define D3DAPPERR_MEDIANOTFOUND       0x8200000b
  32. #define D3DAPPERR_RESIZEFAILED        0x8200000c
  33. #define D3DAPPERR_NULLREFDEVICE       0x8200000d
  34.  
  35.  
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Name: struct D3DModeInfo
  40. // Desc: Structure for holding information about a display mode
  41. //-----------------------------------------------------------------------------
  42. struct D3DModeInfo
  43. {
  44.     DWORD      Width;      // Screen width in this mode
  45.     DWORD      Height;     // Screen height in this mode
  46.     D3DFORMAT  Format;     // Pixel format in this mode
  47.     DWORD      dwBehavior; // Hardware / Software / Mixed vertex processing
  48.     D3DFORMAT  DepthStencilFormat; // Which depth/stencil format to use with this mode
  49. };
  50.  
  51.  
  52.  
  53.  
  54. //-----------------------------------------------------------------------------
  55. // Name: struct D3DDeviceInfo
  56. // Desc: Structure for holding information about a Direct3D device, including
  57. //       a list of modes compatible with this device
  58. //-----------------------------------------------------------------------------
  59. struct D3DDeviceInfo
  60. {
  61.     // Device data
  62.     D3DDEVTYPE   DeviceType;      // Reference, HAL, etc.
  63.     D3DCAPS8     d3dCaps;         // Capabilities of this device
  64.     const TCHAR* strDesc;         // Name of this device
  65.     BOOL         bCanDoWindowed;  // Whether this device can work in windowed mode
  66.  
  67.     // Modes for this device
  68.     DWORD        dwNumModes;
  69.     D3DModeInfo  modes[150];
  70.  
  71.     // Current state
  72.     DWORD        dwCurrentMode;
  73.     BOOL         bWindowed;
  74.     D3DMULTISAMPLE_TYPE MultiSampleTypeWindowed;
  75.     D3DMULTISAMPLE_TYPE MultiSampleTypeFullscreen;
  76. };
  77.  
  78.  
  79.  
  80.  
  81. //-----------------------------------------------------------------------------
  82. // Name: struct D3DAdapterInfo
  83. // Desc: Structure for holding information about an adapter, including a list
  84. //       of devices available on this adapter
  85. //-----------------------------------------------------------------------------
  86. struct D3DAdapterInfo
  87. {
  88.     // Adapter data
  89.     D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
  90.     D3DDISPLAYMODE d3ddmDesktop;      // Desktop display mode for this adapter
  91.  
  92.     // Devices for this adapter
  93.     DWORD          dwNumDevices;
  94.     D3DDeviceInfo  devices[5];
  95.  
  96.     // Current state
  97.     DWORD          dwCurrentDevice;
  98. };
  99.  
  100.  
  101.  
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Name: class CD3DApplication
  105. // Desc: A base class for creating sample D3D8 applications. To create a simple
  106. //       Direct3D application, simply derive this class into a class (such as
  107. //       class CMyD3DApplication) and override the following functions, as 
  108. //       needed:
  109. //          OneTimeSceneInit()    - To initialize app data (alloc mem, etc.)
  110. //          InitDeviceObjects()   - To initialize the 3D scene objects
  111. //          FrameMove()           - To animate the scene
  112. //          Render()              - To render the scene
  113. //          DeleteDeviceObjects() - To cleanup the 3D scene objects
  114. //          FinalCleanup()        - To cleanup app data (for exitting the app)
  115. //          MsgProc()             - To handle Windows messages
  116. //-----------------------------------------------------------------------------
  117. class CD3DApplication
  118. {
  119. protected:
  120.     // Internal variables for the state of the app
  121.     D3DAdapterInfo    m_Adapters[10];
  122.     DWORD             m_dwNumAdapters;
  123.     DWORD             m_dwAdapter;
  124.     BOOL              m_bWindowed;
  125.     BOOL              m_bActive;
  126.     BOOL              m_bReady;
  127.     BOOL              m_bHasFocus;
  128.  
  129.     // Internal variables used for timing
  130.     BOOL              m_bFrameMoving;
  131.     BOOL              m_bSingleStep;
  132.  
  133.     // Internal error handling function
  134.     HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
  135.  
  136.     // Internal functions to manage and render the 3D scene
  137.     HRESULT BuildDeviceList();
  138.     BOOL    FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
  139.                 D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
  140.     HRESULT Initialize3DEnvironment();
  141.     HRESULT Resize3DEnvironment();
  142.     HRESULT ToggleFullscreen();
  143.     HRESULT ForceWindowed();
  144.     HRESULT UserSelectNewDevice();
  145.     VOID    Cleanup3DEnvironment();
  146.     HRESULT Render3DEnvironment();
  147.     virtual HRESULT AdjustWindowForChange();
  148.     static INT_PTR CALLBACK SelectDeviceProc( HWND hDlg, UINT msg, 
  149.                 WPARAM wParam, LPARAM lParam );
  150.  
  151. protected:
  152.     // Main objects used for creating and rendering the 3D scene
  153.     D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset
  154.     HWND              m_hWnd;              // The main app window
  155.     HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)
  156.     HMENU             m_hMenu;             // App menu bar (stored here when fullscreen)
  157.     LPDIRECT3D8       m_pD3D;              // The main D3D object
  158.     LPDIRECT3DDEVICE8 m_pd3dDevice;        // The D3D rendering device
  159.     D3DCAPS8          m_d3dCaps;           // Caps for the device
  160.     D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer
  161.     DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing
  162.     DWORD             m_dwWindowStyle;     // Saved window style for mode switches
  163.     RECT              m_rcWindowBounds;    // Saved window bounds for mode switches
  164.     RECT              m_rcWindowClient;    // Saved client area size for mode switches
  165.  
  166.     // Variables for timing
  167.     FLOAT             m_fTime;             // Current time in seconds
  168.     FLOAT             m_fElapsedTime;      // Time elapsed since last frame
  169.     FLOAT             m_fFPS;              // Instanteous frame rate
  170.     TCHAR             m_strDeviceStats[90];// String to hold D3D device stats
  171.     TCHAR             m_strFrameStats[90]; // String to hold frame stats
  172.  
  173.     // Overridable variables for the app
  174.     TCHAR*            m_strWindowTitle;    // Title for the app's window
  175.     BOOL              m_bUseDepthBuffer;   // Whether to autocreate depthbuffer
  176.     DWORD             m_dwMinDepthBits;    // Minimum number of bits needed in depth buffer
  177.     DWORD             m_dwMinStencilBits;  // Minimum number of bits needed in stencil buffer
  178.     DWORD             m_dwCreationWidth;   // Width used to create window
  179.     DWORD             m_dwCreationHeight;  // Height used to create window
  180.     BOOL              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
  181.     BOOL              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
  182.  
  183.     // Overridable functions for the 3D scene created by the app
  184.     virtual HRESULT ConfirmDevice(D3DCAPS8*,DWORD,D3DFORMAT)   { return S_OK; }
  185.     virtual HRESULT OneTimeSceneInit()                         { return S_OK; }
  186.     virtual HRESULT InitDeviceObjects()                        { return S_OK; }
  187.     virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }
  188.     virtual HRESULT FrameMove()                                { return S_OK; }
  189.     virtual HRESULT Render()                                   { return S_OK; }
  190.     virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }
  191.     virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }
  192.     virtual HRESULT FinalCleanup()                             { return S_OK; }
  193.  
  194. public:
  195.     // Functions to create, run, pause, and clean up the application
  196.     virtual HRESULT Create( HINSTANCE hInstance );
  197.     virtual INT     Run();
  198.     virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  199.     virtual VOID    Pause( BOOL bPause );
  200.  
  201.     // Internal constructor
  202.     CD3DApplication();
  203. };
  204.  
  205.  
  206.  
  207.  
  208. #endif
  209.  
  210.  
  211.  
  212.